<?php
declare (strict_types = 1);

namespace app\index\middleware;

use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\View;

class UserLogin
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //头部请求栏数据中间件
        $code = Cookie::get('index_user');
        $login_status = 0;

        if(isset($code)){  //判断是否存在Cookie

            $user = Cache::get('index_'.$code);
            if(!empty($user)){   //如果获取不到对应的用户缓存就跳回登录
                $login_status = 1;        //登录状态 1登录 0未登录
                View::assign('user',$user);
            }
        }

        View::assign('status',$login_status);


        return $next($request);
    }
}
